package com.example.upload.biz.controller;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.example.common.domain.Result;
import com.example.file.service.impl.MinIOFileStorageService;
import com.example.upload.biz.config.OssProperties;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping
@Tag(name = "图片上传接口")
@RequiredArgsConstructor
public class UploadController {
    private final MinIOFileStorageService uploadService;

    /* private final OSS ossClient;
     private final OssProperties ossProperties;
 */
    @Operation(summary = "单张图片上传")
    @PostMapping("/uploadFile")
    public Result<String> uploadFile(MultipartFile file, String path) throws IOException {
        // 获取原始文件名
        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        // 生成新的文件名--->UUID+文件后缀
        String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));

        String url = uploadService.uploadImgFile(path, newFileName, file.getInputStream());
        // 获取图片的访问路径
        log.info("图片上传成功，访问路径为：{}", url);
        return Result.success(url);
    }

    @Operation(summary = "多张图片上传")
    @PostMapping("/uploadFiles")
    public Result<List<String>> uploadFiles(@RequestPart MultipartFile[] files, @RequestParam("path") String path) throws IOException {
        List<String> urls = Lists.newArrayList();
        for (MultipartFile file : files) {
            // 获取原始文件名
            String originalFilename = file.getOriginalFilename();
            // 生成新的文件名--->UUID+文件后缀
            String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));

            String url = uploadService.uploadImgFile(path, newFileName, file.getInputStream());
            log.info("图片上传成功，访问路径为：{}", url);
            urls.add(url);
        }
        return Result.success(urls);
    }
    @Operation(summary = "字节上传")
    @PostMapping("/uploadBytes")
    public Result<String> uploadBytes(@RequestPart byte[] bytes,@RequestParam String path) throws IOException {

        // path qrCode/18.png
        String[] split = path.split("/");
        // 文件放在split[0]下， 文件名为split[1].png
        String url=null;
        if(split.length == 2){
            url= uploadService.uploadImgFile(split[0], split[1], new ByteArrayInputStream(bytes));
        }else{
            url= uploadService.uploadImgFile("", split[1], new ByteArrayInputStream(bytes));
        }
        log.info("图片上传成功，访问路径为：{}", url);
        return Result.success(url);

    }


  /*  @Operation(summary = "单张图片上传")
    @PostMapping("/uploadFile")
    public Result<String> uploadFile(MultipartFile file, String path) throws IOException {
        String bucketName = ossProperties.getBucketName();
        String endpoint = ossProperties.getEndpoint();

            // 获取原始文件名
            String originalFilename = file.getOriginalFilename();
            // 生成新的文件名--->UUID+文件后缀
            String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));

            // 文件放在path下
            String objectName = path + "/" + newFileName;
            byte[] fileBytes = file.getBytes();
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(fileBytes));
            // 上传文件。
            ossClient.putObject(putObjectRequest);

            // 获取图片的访问路径
            String url = "https://" + bucketName + "." + endpoint + "/" + objectName;
            log.info("图片上传成功，访问路径为：{}", url);
        return Result.success(url);
    }*/


 /*   @Operation(summary = "多张图片上传")
    @PostMapping("/uploadFiles")
    public Result<List<String>> uploadFiles(@RequestPart MultipartFile[] files,  @RequestParam("path")String path) throws IOException {
        List<String> urls = Lists.newArrayList();
        String bucketName = ossProperties.getBucketName();
        String endpoint = ossProperties.getEndpoint();

        for (MultipartFile file : files) {
            // 获取原始文件名
            String originalFilename = file.getOriginalFilename();
            // 生成新的文件名--->UUID+文件后缀
            String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));

            // 文件放在path下
            String objectName = path + "/" + newFileName;
            byte[] fileBytes = file.getBytes();
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(fileBytes));
            // 上传文件。
            ossClient.putObject(putObjectRequest);

            // 获取图片的访问路径
            String url = "https://" + bucketName + "." + endpoint + "/" + objectName;
            log.info("图片上传成功，访问路径为：{}", url);
            urls.add(url);
        }
        return Result.success(urls);
    }
    */

  /*  @Operation(summary = "字节上传")
    @PostMapping("/uploadBytes")
    public Result<String> uploadBytes(@RequestPart byte[] bytes,@RequestParam String path) throws IOException {
        String bucketName = ossProperties.getBucketName();
        String endpoint = ossProperties.getEndpoint();
        String[] split = path.split("/");
        // 文件放在split[0]下， 文件名为split[1].png
        String objectName=null;
        if(split.length == 2){
             objectName = split[0] + "/" + split[1];
        }else{
            objectName =path;
        }
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(bytes));
        // 上传文件。
        ossClient.putObject(putObjectRequest);

        // 获取图片的访问路径
        String url = "https://" + bucketName + "." + endpoint + "/" + objectName;
        log.info("图片上传成功，访问路径为：{}", url);
        return Result.success(url);

    }
*/
}
